/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sg.edu.nus.iss.billing.ui.gui;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Toolkit;
import java.beans.PropertyVetoException;

import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;



import sg.edu.nus.iss.billing.service.login.UserImpl;
import sg.edu.nus.iss.billing.ui.controller.*;
import sg.edu.nus.iss.billing.vo.login.*;

public class LoginWindow extends JFrame {
	
	MainForm main;
	
private OneBillingSystem onebillingsystem;
    public LoginWindow(OneBillingSystem onebillingsystem, MainForm main) {
    	this.main = main;
        this.onebillingsystem=onebillingsystem;
        initComponents();
    }
    
    
    Toolkit tk = Toolkit.getDefaultToolkit();
    Dimension screenSize = tk.getScreenSize();
    int screenHeight = screenSize.height;
    int screenWidth = screenSize.width;

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

    	pnlmain = new JPanel();
        lbluserid = new JLabel("User Id");
        lblpassword = new JLabel("Password");
        txtuserid = new JTextField();
        txtpassword = new JPasswordField();
        btnlogin = new JButton();
        btncancel = new JButton();

        setTitle("One# Billing System -Login");
        setSize(screenWidth / 2, screenHeight / 2);
        setLocation(screenWidth / 4, screenHeight / 4);
        getRootPane().setDefaultButton(btnlogin);
        
        //setClosable(true);
        txtpassword.setEchoChar('*');
        
        pnlmain.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0))));

        btnlogin.setText("Login ");
        
        btnlogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	
            	try{
            	
            	if((txtuserid.getText().equals(""))||(txtuserid.getText().equals(null))){
            		JOptionPane.showMessageDialog(null,"Please enter the user id.","Error",JOptionPane.ERROR_MESSAGE);
            		return;
            	} else if((txtpassword.getPassword().equals(""))||(txtpassword.getPassword().equals(null))) {
            		JOptionPane.showMessageDialog(null,"Please enter the password.","Error",JOptionPane.ERROR_MESSAGE);
            		return;
            	}
            	System.out.println("In Login");
            	LoginVO loginvo = new LoginVO(txtuserid.getText(), new String(txtpassword.getPassword()));
            	UserImpl user= onebillingsystem.login(loginvo);
            	if (user != null)
	            	{
	            		setVisible(false);
	            		main.setVisible(true);
	            		main.SetMenus(user);
	            		main.toFront();
	            		main.setState(Frame.NORMAL);
	
	            		main.pack();
	            	}
            		
            	} catch(Exception e){
            		e.printStackTrace();
            	}
            }


        });
       

        btncancel.setText("Cancel");
        btncancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	try {
					System.exit(1);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
            }


        });
               
        GroupLayout jPanel1Layout = new GroupLayout(pnlmain);
        pnlmain.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnlogin)
                        .addGap(18, 18, 18)
                        .addComponent(btncancel))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.TRAILING)
                            .addComponent(lblpassword)
                            .addComponent(lbluserid))
                        .addGap(40, 40, 40)
                        .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtuserid)
                            .addComponent(txtpassword, GroupLayout.DEFAULT_SIZE, 200, Short.MAX_VALUE))))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(19, 19, 19)
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(lbluserid)
                    .addComponent(txtuserid, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(lblpassword)
                    .addComponent(txtpassword, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                    .addComponent(btnlogin)
                    .addComponent(btncancel))
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        GroupLayout layout = new GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlmain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnlmain, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
                .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

	// Variables declaration - do not modify//GEN-BEGIN:variables
    private JButton btnlogin;
    private JButton btncancel;
    private JLabel lbluserid;
    private JLabel lblpassword;
    private JPanel pnlmain;
    private JPasswordField txtpassword;
    private JTextField txtuserid;
    // End of variables declaration//GEN-END:variables
}
